package sdk

import (
	"fmt"

	"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
	"github.com/hyperledger/fabric-sdk-go/pkg/client/event"
	"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
	"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
)

func (t *SdkChannelInfo) InitService(sdk *fabsdk.FabricSDK, orgId int) error {
	var err error
	org := t.Orgs[orgId]
	// Channel client is used to query and execute transactions
	clientChannelContext := sdk.ChannelContext(t.ChannelID, fabsdk.WithUser(org.OrgUser), fabsdk.WithOrg(org.OrgName))
	t.Client, err = channel.New(clientChannelContext)
	if err != nil {
		fmt.Println("create clientChannelContext error")
		return fmt.Errorf("error while new client: %s", err)
	}

	// create event client
	t.EventClient, err = event.New(clientChannelContext, event.WithBlockEvents())
	if err != nil {
		return fmt.Errorf("error while new event client: %s", err)
	}

	t.LedgerClient, err = ledger.New(clientChannelContext)
	if err != nil {
		return fmt.Errorf("error while new ledger client: %s", err)
	}
	return nil
}
